//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.workflow;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.WorkflowTypeDao;
import com.aidong.model.wills.WorkflowType;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"workflowType"})
public class WorkflowTypeController extends BaseController {
    @Resource
    WorkflowTypeDao workflowTypeDao;

    public WorkflowTypeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("workflow/type/list")).addObject("category", this.getTypeList("workflowCategory"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "category",required = false) String category, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("category", category);
        cd.gt("status", Integer.valueOf(0));
        if(!this.chkNull(name)) {
            ConditionMap mv = new ConditionMap(1);
            mv.like("code", "%" + name + "%");
            mv.like("chName", "%" + name + "%");
            cd.addSub(mv);
        }

        this.workflowTypeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv1 = new ModelAndView("workflow/type/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        WorkflowType workflowType = new WorkflowType();
        workflowType.setNeedAudit1(Integer.valueOf(1));
        workflowType.setNeedAudit2(Integer.valueOf(1));
        workflowType.setNeedAudit3(Integer.valueOf(1));
        workflowType.setStatus(Integer.valueOf(1));
        ModelAndView mv = new ModelAndView("workflow/type/edit");
        mv.addObject("workflowType", workflowType);
        mv.addObject("category", this.getTypeList("workflowCategory"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("workflowType") WorkflowType workflowType) {
        if(this.chkNull(workflowType.getCode())) {
            return this.getResult(false, "请输入编码");
        } else if(this.chkNull(workflowType.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(workflowType.getCategory())) {
            return this.getResult(false, "请选择类别");
        } else {
            if(this.chkNull(workflowType.getNeedAudit1())) {
                workflowType.setNeedAudit1(Integer.valueOf(0));
            }

            if(this.chkNull(workflowType.getNeedAudit2())) {
                workflowType.setNeedAudit2(Integer.valueOf(0));
            }

            if(this.chkNull(workflowType.getNeedAudit3())) {
                workflowType.setNeedAudit3(Integer.valueOf(0));
            }

            if(this.chkNull(workflowType.getStatus())) {
                workflowType.setStatus(Integer.valueOf(1));
            }

            if(workflowType.getNeedAudit1().intValue() == 0 && workflowType.getNeedAudit2().intValue() == 0 && workflowType.getNeedAudit3().intValue() == 0) {
                return this.getResult(false, "至少选择一步审批环节");
            } else if(this.workflowTypeDao.exists(workflowType.getCode())) {
                return this.getResult(false, "编码已存在");
            } else {
                workflowType.setCreateUser(this.getLoginUserCode());
                workflowType.setCreateTime(new Date());
                workflowType.setUpdateTime(new Date());

                try {
                    this.workflowTypeDao.add(workflowType);
                    this.reloadTypeList("workflowType");
                    this.addUserLog("新增审批流类型", "编号:" + workflowType.getCode() + ",名称:" + workflowType.getChName());
                } catch (Exception var3) {
                    this.log.error("add workflowtype", var3);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        WorkflowType workflowType = (WorkflowType)this.workflowTypeDao.getById(code);
        if(workflowType == null) {
            return this.showAlert("没有这个审批流类型");
        } else {
            ModelAndView mv = new ModelAndView("workflow/type/edit");
            mv.addObject("workflowType", workflowType);
            mv.addObject("category", this.getTypeList("workflowCategory"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("workflowType") WorkflowType workflowType) {
        if(this.chkNull(workflowType.getCode())) {
            return this.getResult(false, "没有这个审批流类型");
        } else if(this.chkNull(workflowType.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(workflowType.getCategory())) {
            return this.getResult(false, "请选择类别");
        } else {
            if(this.chkNull(workflowType.getNeedAudit1())) {
                workflowType.setNeedAudit1(Integer.valueOf(0));
            }

            if(this.chkNull(workflowType.getNeedAudit2())) {
                workflowType.setNeedAudit2(Integer.valueOf(0));
            }

            if(this.chkNull(workflowType.getNeedAudit3())) {
                workflowType.setNeedAudit3(Integer.valueOf(0));
            }

            if(this.chkNull(workflowType.getStatus())) {
                workflowType.setStatus(Integer.valueOf(1));
            }

            if(workflowType.getNeedAudit1().intValue() == 0 && workflowType.getNeedAudit2().intValue() == 0 && workflowType.getNeedAudit3().intValue() == 0) {
                return this.getResult(false, "至少选择一步审批环节");
            } else {
                WorkflowType workflowType1 = (WorkflowType)this.workflowTypeDao.getById(workflowType.getCode());
                if(workflowType1 == null) {
                    return this.getResult(false, "没有这个审批流类型");
                } else {
                    workflowType1.setChName(workflowType.getChName());
                    workflowType1.setCategory(workflowType.getCategory());
                    workflowType1.setNeedAudit1(workflowType.getNeedAudit1());
                    workflowType1.setNeedAudit2(workflowType.getNeedAudit2());
                    workflowType1.setNeedAudit3(workflowType.getNeedAudit3());
                    workflowType1.setNote(workflowType.getNote());
                    workflowType1.setStatus(workflowType.getStatus());
                    workflowType1.setUpdateTime(new Date());

                    try {
                        this.workflowTypeDao.update(workflowType1);
                        this.reloadTypeList("workflowType");
                        this.addUserLog("修改审批流类型", "编号:" + workflowType1.getCode() + ",名称:" + workflowType1.getChName());
                    } catch (Exception var4) {
                        this.log.error("edit workflowtype", var4);
                        return this.getResult(false, "保存发生错误");
                    }

                    return this.getResult(true, "保存完成");
                }
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        WorkflowType workflowType = (WorkflowType)this.workflowTypeDao.getById(code);
        if(workflowType == null) {
            return this.getResult(false, "没有这个审批流类型");
        } else {
            try {
                this.workflowTypeDao.updateStatus(code, 0);
                this.reloadTypeList("workflowType");
                this.addUserLog("删除审批流类型", "编号:" + workflowType.getCode() + ",名称:" + workflowType.getChName());
            } catch (Exception var4) {
                this.log.error("del workflowtype", var4);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
